package com.zzh.api.order.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zzh.api.base.response.BaseResponse;
import com.zzh.api.base.service.BaseApiService;
import com.zzh.api.feign.MemberServiceFeign;
import com.zzh.api.member.entity.MemberEntity;
import com.zzh.api.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zihong.zheng
 * @version 1.0
 * @date 2019/2/8
 * @time 0:22
 * @description
 */
@RestController
public class OrderServiceImpl extends BaseApiService implements IOrderService {

    @Autowired
    private MemberServiceFeign memberServiceFeign;

    @RequestMapping("/orderToMember")
    public String orderToMember(String name) {
        MemberEntity memberEntity = memberServiceFeign.getMember(name);
        return memberEntity == null ? "没有找到用户" : memberEntity.toString();
    }



    @RequestMapping("/orderToMemberInfo")
    public BaseResponse orderToMemberInfo() {
        // 线程名称就是通过线程池名称+线程id组合的
        System.out.println("orderToMemberInfo: 当前线程名称："+ Thread.currentThread().getName());
        return memberServiceFeign.orderToMemberInfo();
    }


    /**
     * 未解决雪崩效应接口
     * @return
     */
    @RequestMapping("/orderToMemberGetOrderInfo")
    public BaseResponse orderToMemberGetOrderInfo() {
        System.out.println("orderToMemberInfo: 当前线程名称："+ Thread.currentThread().getName());
        return memberServiceFeign.getUserInfo();

    }


    /**
     * HystrixCommand 注解默认开启线程隔离、服务降级、服务熔断机制
     *设置Hystrix超时时间
     * HystrixCommand 注解默认开启线程隔离 以线城池方式
     *                  默认开启服务降级执行方法 orderToMemberGetOrderInfoForHystrixFallBackMethod
     *                    默认开启服务熔断机制
     * @return
     */
    @HystrixCommand(fallbackMethod = "orderToMemberGetOrderInfoForHystrixFallBackMethod" )
    @RequestMapping("/orderToMemberGetOrderInfoForHystrix")
    public BaseResponse orderToMemberGetOrderInfoForHystrix() {
        System.out.println("orderToMemberGetOrderInfoForHystrix: 当前线程名称："+ Thread.currentThread().getName());
        return memberServiceFeign.getUserInfo();

    }


    /**
     * 服务降级另外一种调用
     * @return
     */
    @RequestMapping("/orderToMemberGetOrderInfoForHystrix2")
    public BaseResponse orderToMemberGetOrderInfoForHystrix2() {
        System.out.println("orderToMemberGetOrderInfoForHystrix: 当前线程名称："+ Thread.currentThread().getName());
        return memberServiceFeign.getUserInfo();

    }

    /**
     * fallbackMethod 方法含义，服务降级执行，返回友好提示
     * @return
     */
    public BaseResponse orderToMemberGetOrderInfoForHystrixFallBackMethod(){
        return returnSuccess("系统繁忙，清稍后重试");
    }

    @RequestMapping("/orderInfo")
    public BaseResponse orderInfo(){
        System.out.println("orderInfo: 当前线程名称："+ Thread.currentThread().getName());
        return returnResultSuccess();
    }

}
